package com.oocl.myHomework.service;

import com.oocl.myHomework.dao.CRMDao;
import com.oocl.myHomework.dao.CRMDaoImpl;
import com.oocl.myHomework.entity.Person;
import com.oocl.myHomework.list.DoubleLinkedList;

public class CRMServiceImpl implements CRMService{
     public static DoubleLinkedList<Person> personList;
     
    public void initData(){
    	CRMDao crmDao = new CRMDaoImpl();
    	personList = crmDao.loadPersons();
    	 
     }

	public Person[] find() throws Exception {
		Person[] persons = new Person[personList.getSize()];
		for(int i=0;i<personList.getSize();i++){
			persons[i] = personList.getElementByIndex(i);
		}
		return persons;
		
 	}
	
	public Person[] findByName(String name) throws Exception {
		// TODO Auto-generated method stub
		
		Person[] persons = new Person[personList.getSize()];
		int n =0 ;
		for(int i=0;i<personList.getSize();i++){
			Person  person = personList.getElementByIndex(i);
			if(name.equals(person.getName())){
				persons[n] = person;
				n++;
			}
		}
		return persons;
	}

	public void addPerson(Person person) throws Exception {
		// TODO Auto-generated method stub
		personList.addAtEnd(person);
	}

	public int getMaxId() throws Exception {
		// TODO Auto-generated method stub
		Person person = personList.getElementByIndex(personList.getSize()-1);
		return person.getId();
	}

	public void delete(Person person) {
		// TODO Auto-generated method stub
		personList.delete(person);
	}

	public void update(Person p) throws Exception {
		// TODO Auto-generated method stub
		int index = personList.IndexOf(p);
		personList.update(p, index);
	}
     
     
}
